package com.zls.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;

@RestController
@RequestMapping("/sse")
public class SseController {
//    @GetMapping(value = "get")
//    public void getData_True(HttpServletResponse response){
//        response.setContentType("text/event-stream");
//        response.setCharacterEncoding("utf-8");
//
//        try{
//            PrintWriter pw = response.getWriter();
//            while (true){
//                if (pw.checkError()){
//                    System.out.println("客户端断开连接");
//                    break;
//                }
//                Thread.sleep(1000);
//                pw.write("data:行情:" + Math.random()+"\n\n");
//                pw.flush();
//            }
//        }catch (IOException | InterruptedException e){
//            e.printStackTrace();
//        }
//    }

    private static Map<String, SseEmitter> sseCache = new ConcurrentHashMap<>();
    // https://cloud.tencent.com/developer/article/1620176
    @GetMapping(path = "subscribe")
    public SseEmitter push(String id) {
        // 超时时间设置为1小时
        SseEmitter sseEmitter = new SseEmitter(3600_000L);
        sseCache.put(id, sseEmitter);
        sseEmitter.onTimeout(() -> sseCache.remove(id));
        sseEmitter.onCompletion(() -> System.out.println("完成！！！"));

        Executors.newFixedThreadPool(1).submit(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    SseEmitter sseEmitter = sseCache.get(id);
                    if (sseEmitter != null) {
                        try {
                            sseEmitter.send(Math.random());
                        } catch (IOException e) {
                            throw new RuntimeException(e);
                        }
                    }
                }
            }
        });
        return sseEmitter;
    }

    @GetMapping(path = "push")
    public String push(String id, String content) throws IOException {
        SseEmitter sseEmitter = sseCache.get(id);
        if (sseEmitter != null) {
            sseEmitter.send(content);
        }
        return "over";
    }

    @GetMapping(path = "over")
    public String over(String id) {
        SseEmitter sseEmitter = sseCache.get(id);
        if (sseEmitter != null) {
            sseEmitter.complete();
            sseCache.remove(id);
        }
        return "over";
    }
}
